#!/usr/bin/env python

import sys, os
import csv

def usage():
    print '''Usage: %s submissions.csv

    - Go to the Conference Reporting.
    - Choose Submission List
    - Select the program and choose "Complete" reviews
    - Export to submissions.csv file

Running this program will generate a map file from the .csv file so that the
web interface can see the submission title rather than the cryptic submission
numbers.''' % sys.argv[0]
    sys.exit(1)


def main():
    if len(sys.argv) < 2:
        usage()
    fp = open(sys.argv[1], 'r')
    sheet = csv.reader(fp)
    # Process title row
    for row in sheet:
        if row[0] != 'Submission' \
                            or row[1] != 'Status' \
                            or row[2] != 'Curated' \
                            or row[6] != 'Title':
            print 'Unexpected title row:', row
            usage()
        break

    subs = {}
    for row in sheet:
        if len(row) < 7:
            print 'Skip row:', row
            continue
        sub = row[0]
        stat = row[1]
        style = row[2]
        title = row[6]
        if stat != 'COMPLETE' or style != 'Juried':
            print 'Skip', sub, '(Reasons:', stat, style, ')'
            continue
        if subs.has_key(sub):
            if title != subs[sub]:
                print 'Title mis-match for submission', sub
        else:
            subs[sub] = title

    slist = []
    for s in subs:
        slist.append('%s %s\n' % (s, subs[s]))
    slist.sort()
    fp = open('map-names.txt', 'w')
    for s in slist:
        fp.write(s)
    print 'Saved %d submission titles to map-names.txt' % len(slist)

if __name__ == "__main__":
    main()
